/* Assignment Notebook
 * Created By: Amanda Rush
 * www.AmandaRush.com
 * Version 1.1
 * Release: 4/14/2011
 */

package com.captainkat.assignmentnotebook;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.content.Intent;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.Calendar;
import java.util.TimeZone;
import android.widget.TextView;
import java.text.SimpleDateFormat;



import android.net.Uri;
import android.database.Cursor;
import android.content.ContentUris;
import android.content.ContentValues;


public class AssignmentView extends Activity implements OnClickListener {
	//CALENDAR THINGS
	protected TextView date;
	protected TextView startTime;
	protected TextView endTime;
	protected TextView title;
	protected TextView complete;
	protected TextView desc;
	protected TextView edit;
	protected TextView back;
	protected Calendar currentDate;
	protected Calendar assignmentDate;
	protected Calendar assignmentEndDate;
	protected TimeZone timezone;
	protected SimpleDateFormat dateFormat;
	protected SimpleDateFormat timeFormat;
	protected String calendarURI;
	
	//get calendar Uri
	protected Uri calendars;
	//set the calendar id
	protected String calendarId = "-1";
	//get events uri
	protected Uri eventsUri;
	//gets instances uri
	protected Uri instancesUri;
	//event instances
	protected EventHolder eventHolder;
	
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setRequestedOrientation(1);  
		setContentView(R.layout.assignment);
		
		checkVersion();
		
		//EventHolder
		eventHolder = new EventHolder();
		
		//Set up the date
		currentDate = Calendar.getInstance();
		assignmentDate= Calendar.getInstance();
		assignmentEndDate =Calendar.getInstance();
		
		//GET OUT OF BUNDLE
		Bundle b = this.getIntent().getExtras();
		calendarId = b.getString("calendarId");
		currentDate.setTimeInMillis(b.getLong("time"));
		eventHolder.eid = b.getString("eid");
		timezone = TimeZone.getDefault();
		currentDate.setTimeZone(timezone);
		assignmentDate.setTimeZone(timezone);
		dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		timeFormat = new SimpleDateFormat("HH:mm");
	
		//set up views and click listeners
		date = (TextView) findViewById(R.id.asnDate);
		date.setOnClickListener(this);
		startTime =(TextView) findViewById(R.id.asntime);
		startTime.setOnClickListener(this);
		endTime = (TextView) findViewById(R.id.asnendtime);
		endTime.setOnClickListener(this);
		title = (TextView) findViewById(R.id.asnTitle);
		title.setOnClickListener(this);
		complete = (TextView) findViewById(R.id.chkComplete);
		complete.setOnClickListener(this);
		desc = (TextView) findViewById(R.id.description);
		desc.setOnClickListener(this);
		edit = (TextView) findViewById(R.id.btnEdit);
		edit.setOnClickListener(this);
		back = (TextView) findViewById(R.id.btnBack);
		back.setOnClickListener(this);
		
		//disable edit button in this version
		//edit.setEnabled(false);
		
		drawScreen();
	}
	
	public void drawScreen() {
		//clear the Assignments
		title.setText("");
		desc.setText("");
		
		//put the date on the screen
		date.setText(dateFormat.format(currentDate.getTime()));
		
		
		//Build instances uri for ONE DAY 
		Uri.Builder builder = Uri.parse("content://"+calendarURI+"/instances/when").buildUpon();
		long daysofar = (currentDate.getTimeInMillis() + timezone.getOffset(currentDate.getTimeInMillis())) % DateUtils.DAY_IN_MILLIS;
		long dayleft = DateUtils.DAY_IN_MILLIS - daysofar;
		ContentUris.appendId(builder, currentDate.getTimeInMillis() - daysofar);
		ContentUris.appendId(builder, currentDate.getTimeInMillis() + dayleft);
		
		//make selection and projection
		String[] projection = new String[] {"calendar_id","event_id","title","description" ,"dtstart","dtend" };
		String selection = "event_id="+eventHolder.eid;
		
		Cursor managedCursor = managedQuery(builder.build(), projection, selection,null,null);
		
		if (managedCursor.moveToFirst()) {
			//check
			int calIdColumn = managedCursor.getColumnIndex("calendar_id");
			int startColumn = managedCursor.getColumnIndex("dtstart");
			int endColumn = managedCursor.getColumnIndex("dtend");
			int titleColumn = managedCursor.getColumnIndex("title");
			int descColumn = managedCursor.getColumnIndex("description");
			
			
			//save calendar Id
			eventHolder.calendarId = managedCursor.getString(calIdColumn);
			//set Calendar Id
			calendarId= eventHolder.calendarId;
			//get start date + time
			eventHolder.dtstart = managedCursor.getLong(startColumn);
			eventHolder.dtend = managedCursor.getLong(endColumn);
			//save dates
			assignmentDate.setTimeInMillis(eventHolder.dtstart);
			assignmentEndDate.setTimeInMillis(eventHolder.dtend);
			//show dates
			date.setText(dateFormat.format(assignmentDate.getTime()));
			startTime.setText(timeFormat.format(assignmentDate.getTime()));
			endTime.setText(timeFormat.format(assignmentEndDate.getTime()));

			//get title
			eventHolder.title = managedCursor.getString(titleColumn);
			//get description
			eventHolder.description = managedCursor.getString(descColumn);
			//set text
			title.setText(eventHolder.title);	
			desc.setText(eventHolder.description);
			
		}
		
		//check complete status, disable checker
		if (title.getText().length() >0)
			if (title.getText().charAt(0) == '\u2713')
				complete.setEnabled(false);
		
		
	}
	
	@Override
	public void onClick(View v) {
		Uri updateEventsUri = ContentUris.withAppendedId(eventsUri, Long.parseLong(eventHolder.eid));
		// TODO Auto-generated method stub
		Bundle b;
		switch(v.getId()) {
		
	
		case R.id.chkComplete:
			//STRIKE THROUGH.. NEED TO WRITE!
			title.setText('\u2713'+ title.getText().toString());
			// save it
			ContentValues event = new ContentValues();
			//set which calendar
			event.put("calendar_id", calendarId);
			//put event data in 
			event.put("title", title.getText().toString());
			
			//Update the database
			getContentResolver().update(updateEventsUri,event,null,null);
			drawScreen();


			break;
		
		case R.id.btnEdit:
			Intent edit = new Intent(v.getContext(), AssignmentEditExisting.class);
			//add date and stuff once you have this function
			b = new Bundle();
			b.putLong("time",assignmentDate.getTimeInMillis());
			b.putString("eid",eventHolder.eid);
			b.putString("calendarId", calendarId);
			edit.putExtras(b);
            startActivity(edit);
            finish();
			break;
		case R.id.btnBack:
			Intent back = new Intent(v.getContext(), DailyView.class);
			b = new Bundle();
			b.putLong("time",currentDate.getTimeInMillis());
			back.putExtras(b);
            startActivity(back);
            finish();
			break;
			
		}
		
		
		}
	public void checkVersion() {
		if (Integer.parseInt(Build.VERSION.SDK) <=7) //if lower than or equal to Android 2.1
			calendarURI = "calendar";
		else
			calendarURI = "com.android.calendar";
		
		//put in the correct content provider for that version
		calendars = Uri.parse("content://"+calendarURI+"/calendars");
		eventsUri = Uri.parse("content://"+calendarURI+"/events");
		instancesUri = Uri.parse("content://"+calendarURI+"/instances/when");
		
	}
	
	

	}

